package de.tu_darmstadt.gdi1.xcom.tests.adapters;

import de.tu_darmstadt.gdi1.framework.utils.Point;
import de.tu_darmstadt.gdi1.xcom.controller.events.ActionEnum;
import de.tu_darmstadt.gdi1.xcom.controller.events.GameStateEnum;
import de.tu_darmstadt.gdi1.xcom.controller.events.XcomActionEvent;
import de.tu_darmstadt.gdi1.xcom.model.elements.Human;

/**
 * This is the testadapter for testcases for the second level of completion.
 * Implement the empty methods in this adapter and match them to your
 * concrete implementation of the game.
 * 
 * Note: This adapter extends the level1-testadapter
 * 
 * @author Lukas Schwoebel
 * @author Waldemar Ankudin
 * @author Michael Zinn
 *
 */

public class XcomTestAdapterLevel2 extends XcomTestAdapterLevel1 {

	/**
	 * return a savegame with the current gamestate. The savegame should have
	 * the same format as the levelfile. See in the game-document for the
	 * needed format.
	 */
	public String returnGamestateAsSavegame() {
		// TODO: fill stub
		return testImpl.getSavegame();
	}
	
	/**
	 * perform the given KI-Action by processing the action-string
	 * see in the game-document for the specified format
	 * NOTE: you can be sure, that if this method is called, it is the turn for the KI/aliens.
	 * Before any action for human players is performed, there will be an "endTurn()".
	 * There will be no mixture between KI-actions and human-actions in the same turn!
	 * Anyways, the number of turns left should not decrease if this method is called.
	 */
	public void performKIAction(String kiActionString) {
		// TODO: fill stub

		if(testImpl.getGd().getGameState() != GameStateEnum.RUNNING)
			return;
		
		String[] getKIAction = kiActionString.split(";");
		int x = Integer.parseInt(getKIAction[0]);
		int y = Integer.parseInt(getKIAction[1]);
		
		if(!testImpl.getGd().getTeam(false).getMobPositions().contains(new Point(x, y)))
			return;
		
		if(testImpl.getGd().isTeam1IsActive())
			testImpl.getGd().changeTeam();
		
		
		testImpl.getKeyFromMouse(x, y);
		switch(getKIAction[2].charAt(0)) {
			case 'l': 
				testImpl.processEvent(new XcomActionEvent(ActionEnum.RotateLeft)); break;
			case 'r': 
				testImpl.processEvent(new XcomActionEvent(ActionEnum.RotateRight)); break;
			case 'f':
				testImpl.processEvent(new XcomActionEvent(ActionEnum.Move)); break;
			 case 'a':
				testImpl.processEvent(new XcomActionEvent(ActionEnum.Attack)); break;
		}
	}

	/**
	 * return the number of turns left to play
	 * NOTE: the current turn does count, too. So if there is listed maxrounds:2 in the
	 * gamestate, return 2 in the first turn, even if some actions were performed already.
	 */
	public int turnsLeft() {
		// TODO: fill stub
		return Math.max(0, testImpl.getGd().getMaxRounds() - testImpl.getGd().getPlayedRounds());
	}
	
	/**
	 * return the amount of ammo/munitions left for the active player
	 */
	public int ammoLeft() {
		// TODO: fill stub
		return ((Human)testImpl.getGd().getActiveTeam().getCurrentMob()).getMunition();
	}
	
	/**
	 * execute a shoot for the active player.
	 */
	public void shoot() {
		// TODO: fill stub
		testImpl.processEvent(new XcomActionEvent(ActionEnum.Shoot));
	}
	
	/**
	 * return how many healthpoints an alien on the given position has left
	 */
	public int healthPointsOfAlien(int x, int y) {
		// TODO: fill stub
		return testImpl.getGd().getMobOnPosition(new Point(x, y)).getHealth();
	}

	/**
	 * return how many healthpoints are left for the active player
	 */
	public int healthPointsLeft() {
		// TODO: fill stub
		return testImpl.getGd().getActiveTeam().getCurrentMob().getHealth();
	}
}
